function [E, vals] = slprefilter_edges(E, vals, f)
%SLPREFILTER_EDGES filters the edge set and take actions on the edges with
%multiple occurrences
%
% [ Syntax ]
%   - [E, vals] = slprefilter_edges(E, vals, f)
%   
% [ History ]
%   - Created by Dahua Lin, on Jul 5, 2007
%

%% sort by edge index

[E, I] = sort(E);
vals = vals(I);

%% find repeated ranges

diffs = diff(E) > 0;
ed1 = [true; diffs];
ed2 = [diffs; true];

rgns = find(xor(ed1, ed2));

%% do filtering

if ~isempty(rgns)
    E0 = E(ed1 & ed2);
    v0 = vals(ed1 & ed2);
    
    E1 = E(rgns(1:2:end));
    v1 = arrayfun(@(u, v) f(vals(u:v)), rgns(1:2:end), rgns(2:2:end));
    
    E = [E0; E1];
    vals = [v0; v1];
end
